package cn.bjxy.shishaolong.system.user.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Collect;
import cn.bjxy.shishaolong.request_answer.entity.Report;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.request_answer.service.IAnswerService;
import cn.bjxy.shishaolong.request_answer.service.ICollectService;
import cn.bjxy.shishaolong.request_answer.service.IReportService;
import cn.bjxy.shishaolong.request_answer.service.IRequestService;
import cn.bjxy.shishaolong.system.annotation.Permission;
import cn.bjxy.shishaolong.system.controller.AbstractController;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.user.entity.SysUserInfo;
import cn.bjxy.shishaolong.system.user.entity.SysUserLocalAuthen;
import cn.bjxy.shishaolong.system.user.service.ISysUserInfoService;
import cn.bjxy.shishaolong.system.user.service.ISysUserLocalAuthenService;
import cn.bjxy.shishaolong.system.user.service.IUserMainService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户控制器
 *
 * @Author 时少龙
 * @Date 2019-04-02 9:53
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/api/web/user-main")
@Slf4j
public class UserMainController extends AbstractController {

    @Autowired
    private IUserMainService userMainService;

    @Autowired
    private IRequestService requestService;
    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IReportService reportService;

    @Autowired
    private ICollectService collectService;

    /**
     * 获取用户相关的统计信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryUserRelateCount")
    @Permission
    public ResultMsg queryUserRelateCount(HttpServletRequest request) {
        try {
            Integer currentUserId = getCurrentUserId(request);
            Map map = userMainService.queryUserRelateCountByUserId(currentUserId);
            return ResultMsg.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SERVER_INNER_ERROR, StatusCode.SERVER_INNER_ERROR_MSG);
        }
    }

    /**
     * 查询问题
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryRequest")
    @Permission
    public ResultMsg queryRequest(HttpServletRequest httpRequest, @RequestBody JSONObject param) {
        try {
            //查询条件
            Request request = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                request = param.getObject(SysConst.QUERY_CONDITION_KEY, Request.class);
            } else {
                request = new Request();
            }
            Integer currentUserId = getCurrentUserId(httpRequest);
            request.setUserId(currentUserId);
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Request> requestPage = new Page<>();
                requestPage.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Request> list = requestService.queryByCondition(requestPage, request);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Request> list = requestService.queryByCondition(request);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 获取用户回答
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryAnswer")
    @Permission
    public ResultMsg queryAnswer(HttpServletRequest httpRequest, @RequestBody JSONObject param) {
        try {
            //查询条件
            Answer answer = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                answer = param.getObject(SysConst.QUERY_CONDITION_KEY, Answer.class);
            } else {
                answer = new Answer();
            }
            Integer currentUserId = getCurrentUserId(httpRequest);
            answer.setUserId(currentUserId);
            answer.setAnswerOwn(SysConst.ANSWER_PLAIN);
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Answer> answerPage = new Page<>();
                answerPage.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Answer> list = answerService.queryByCondition(answerPage, answer);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Answer> list = answerService.queryByCondition(answer);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 获取用户举报
     *
     * @param httpRequest
     * @return
     */
    @RequestMapping("/queryReport")
    @Permission
    public ResultMsg queryReport(HttpServletRequest httpRequest, @RequestBody JSONObject param) {
        try {
            //查询条件
            Report report = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                report = param.getObject(SysConst.QUERY_CONDITION_KEY, Report.class);
            } else {
                report = new Report();
            }
            Integer currentUserId = getCurrentUserId(httpRequest);
            report.setUserId(currentUserId);
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Report> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Report> list = reportService.queryByCondition(page, report);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Report> list = reportService.queryByCondition(report);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 获取用户收藏
     *
     * @param httpRequest
     * @return
     */
    @RequestMapping("/queryCollect")
    @Permission
    public ResultMsg queryCollect(HttpServletRequest httpRequest, @RequestBody JSONObject param) {
        try {
            //查询条件
            Collect collect = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                collect = param.getObject(SysConst.QUERY_CONDITION_KEY, Collect.class);
            } else {
                collect = new Collect();
            }
            Integer currentUserId = getCurrentUserId(httpRequest);
            collect.setUserId(currentUserId);
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Collect> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
                IPage<Collect> list = collectService.queryByCondition(page, collect);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Collect> list = collectService.queryByCondition(collect);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 收藏跳转详情
     *
     * @return
     */
    @PostMapping("/collectDetail")
    public ResultMsg collectDetail(Integer reqAnsType, Integer requestId, Integer reqAnsId) {
        try {
            if (StringUtils.isEmpty(reqAnsType) || StringUtils.isEmpty(requestId) || StringUtils.isEmpty(reqAnsId)) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            Request request = userMainService.collectDetail(reqAnsType, requestId, reqAnsId);
            return ResultMsg.success(request);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }
}
